<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","skada_id");
$FileName       = "ais_setup_budget.php";
$FileTemplate   = "ais_setup_budget_form.html";
$SubHeaderTitle = "Setup Budget List";
$TableName		= "tblcoa";
$HTMLFormName   = "COAForm";
//$HTMLFormName2  = "BudgetForm";


$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("skada"=>"0","namacc"=>"0","bgt01"=>"number","bgt02"=>"number","bgt03"=>"number","bgt04"=>"number","bgt05"=>"number","bgt06"=>"number","bgt07"=>"number","bgt08"=>"number","bgt09"=>"number","bgt10"=>"number","bgt11"=>"number","bgt12"=>"number");
buildsqlfield ("$TableName", $FormFields,1);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
	if (GetParam("Action","")=="Add") {
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		$FormMode="Edit";
		FormUpdate();
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
//$Verification  = "False";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
//$VerifyFields  = array("kdreceipt"=>"Receipt Code","receiptname"=>"Receipt Name");
//VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);
$InitFunction = "Masking()";
Init($SessionVar,BuildMasking ($HTMLFormName,$FormFields));

//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}

//Select Department 
$SQLDept = "SELECT * FROM tbldept";

//print $SQLForm;
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

if (GetParam(PRIMARY_KEY,"")) {$TBS->MergeBlock("blv","mysql",$SQLForm);}
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection;
	$DBConnection->Update();
}

function FormDelete() {
	global $DBConnection,$FileName,$TableName;
	$DBConnection->Delete("$TableName",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield, $isnumber=0) {
		global $DBConnection;
		if ($isnumber) {
			foreach ($arrayfield as $key=>$Formatting) {
				$value = GetParam($key,"");
				if ($Formatting=="number") $value = str_replace(",","",$value);
				$DBConnection->add_htmlfield($table,$key,$value);
			}			
		} else {
			for ($x=0;$x<count($arrayfield);$x++) {
				$DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
			}
	    }
}

?>